package zhang.lao.service.console.base;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import zhang.lao.build.kit.LogKit;
import zhang.lao.build.mybatis.jdbc.auto.model.WnMachine;
import zhang.lao.build.mybatis.jdbc.auto.model.WnMachineDevice;
import zhang.lao.build.tool.BeanMapTool;
import zhang.lao.dao.base.WnFarmDao;
import zhang.lao.dao.base.WnMachineDao;
import zhang.lao.dao.base.WnMachineDeviceDao;
import zhang.lao.dao.base.WnMachineWorkReportAutoDao;
import zhang.lao.build.mybatis.jdbc.auto.model.WnMachineWorkReportAuto;
import zhang.lao.build.mybatis.jdbc.auto.model.WnMachineWorkReportAutoExample;
import zhang.lao.build.tool.LzStringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import zhang.lao.build.mybatis.jdbc.auto.tool.ControllerQueryTool;
import zhang.lao.pojo.console.req.BootStrapGridReq;
import zhang.lao.pojo.console.resp.BootStrapGridResp;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.pojo.console.resp.HttpResult;
import javax.annotation.Resource;
import java.util.List;
import zhang.lao.build.tool.UUIDTool;
/**
* 
* @author 
* @create 
**/
 @Service
public class WnMachineWorkReportAutoService{

	@Resource
	private WnFarmDao wnFarmDao;
	@Resource
	private WnMachineDeviceDao wnMachineDeviceDao;
	@Resource
	private WnMachineDao wnMachineDao;
	@Resource
	private WnMachineWorkReportAutoDao wnMachineWorkReportAutoDao;

	public String add(){
		return "console/wnMachineWorkReportAuto/wnMachineWorkReportAuto_form";
	}

	public String edit(ModelMap modelMap,java.lang.String id){
			modelMap.put("wnMachineWorkReportAuto", wnMachineWorkReportAutoDao.selectByPrimaryKey(id));
		return "console/wnMachineWorkReportAuto/wnMachineWorkReportAuto_form";
	}

	public String list(){
		return "console/wnMachineWorkReportAuto/wnMachineWorkReportAuto_table";
	}

	public BootStrapGridResp json(BootStrapGridReq bootGridReq){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		WnMachineWorkReportAutoExample wnMachineWorkReportAutoExample = new WnMachineWorkReportAutoExample();
        ControllerQueryTool.setWnMachineWorkReportAutoCriteria(bootGridReq.getQuery(),wnMachineWorkReportAutoExample.createCriteria());
		List<WnMachineWorkReportAuto> wnMachineWorkReportAutoList = wnMachineWorkReportAutoDao.selectByExample(wnMachineWorkReportAutoExample);
		List<JSONObject> list = BeanMapTool.objectsToJsonObjects(wnMachineWorkReportAutoList);
		if(list!=null) {
			for (JSONObject jsonObject : list) {
				jsonObject.put("farmName", wnFarmDao.selectByPrimaryKey(jsonObject.getString("farmId")).getName());
				jsonObject.put("deviceNumber", wnMachineDeviceDao.selectByPrimaryKey(jsonObject.getInteger("deviceId")).getDeviceNumber());
				WnMachine wnMachine = wnMachineDao.selectByPrimaryKey(jsonObject.getString("machineId"));
				if (wnMachine != null) {
					jsonObject.put("machineName", wnMachine.getLicensePlateNumber());

				} else {
					jsonObject.put("machineName", "未绑定农机");
				}

			}
		}

		return new BootStrapGridResp(page.getTotal(),list);
	}

	public HttpResult save(String formObjectJson){
		WnMachineWorkReportAuto wnMachineWorkReportAuto= JSON.parseObject(formObjectJson,WnMachineWorkReportAuto.class);
			java.lang.String id=wnMachineWorkReportAuto.getId();
		if (id!=null) {
			wnMachineWorkReportAutoDao.updateByPrimaryKeySelective(wnMachineWorkReportAuto);
		}else{
			wnMachineWorkReportAuto.setId(UUIDTool.getUUID());
			wnMachineWorkReportAutoDao.insertSelective(wnMachineWorkReportAuto);
		}
		return HttpResultUtil.buildSuccess();

	}

	@Transactional
	public HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
		wnMachineWorkReportAutoDao.deleteByPrimaryKey(java.lang.String.valueOf(id));
		}
		return HttpResultUtil.buildSuccess();
	}
}
